package io.shallwe.scanner.auto;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.tio.core.Tio;
import org.tio.utils.SystemTimer;
import com.jfinal.kit.LogKit;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import io.shallwe.scanner.manual.MessagePacket;

public class AutoScanner {

	private volatile boolean rackArriveEnterance=false;
	private volatile boolean rackArriveExit=false;
	
	private ReentrantLock enterancelock = null;
	private ReentrantLock exitlock = null;
	public static final String ENTER="enter";
	public static final String EXIT="exit";
	public long lastRackArriveEnteranceScanTime= SystemTimer.currTime;
	public long lastRackArriveExitScanTime= SystemTimer.currTime;
	private SiemensS7Net plc=null;
	private volatile boolean isStop=true;
	
	private MessagePacket readBarcodeCommandPacket=null;
	
	private AutoEnterScannerClient autoEnterScannerClient;
	private AutoEixtScannerClient autoEixtScannerClient;
	public AutoScanner() {
		enterancelock=new ReentrantLock();
		exitlock=new ReentrantLock();
		plc=new SiemensS7Net(SiemensPLCS.S1500,"172.30.31.1");
		readBarcodeCommandPacket=new MessagePacket();
		byte[] readBarcodeCommand= {0X1B,0X4C,0X4F,0X4E,0X0D};
		readBarcodeCommandPacket.setBody(readBarcodeCommand);
		
		autoEnterScannerClient=new AutoEnterScannerClient();
		autoEixtScannerClient=new AutoEixtScannerClient();
	}
	
	public void setRackArriveEnterance(boolean rackArriveEnterance) {
		LogKit.warn(String.format("入口接近开关 本次至为%s,上次值为%s", rackArriveEnterance,this.rackArriveEnterance));
		try {
			enterancelock.tryLock(1, TimeUnit.SECONDS);
			if(rackArriveEnterance!=this.rackArriveEnterance) {
				this.rackArriveEnterance=rackArriveEnterance;
				if(rackArriveEnterance) {
					Tio.send(AutoEnterScannerClient.clientChannelContext,
							readBarcodeCommandPacket);
				}
			}
		} catch (Throwable e) {
			LogKit.error(e.getMessage());
		} finally {
			enterancelock.unlock();
		}
	}
	
	public void setRackArriveExit(boolean rackArriveExit) {
		LogKit.warn(String.format("入口接近开关 本次至为%s,上次值为%s", rackArriveExit,this.rackArriveExit));
		try {
			exitlock.tryLock(1, TimeUnit.SECONDS);
			if(rackArriveExit!=this.rackArriveExit) {
				this.rackArriveExit=rackArriveExit;
				if(rackArriveExit) {
					Tio.send(AutoEixtScannerClient.clientChannelContext,
							readBarcodeCommandPacket);
				}
			}
		} catch (Throwable e) {
			LogKit.error(e.getMessage());
		} finally {
			exitlock.unlock();
		}
	}
	
	/**
	 * 扫描头启动
	 */
	public void start() {
		autoEnterScannerClient.start();
		autoEixtScannerClient.start();
		
		plc.ConnectServer();
		plc.SetPersistentConnection();
		isStop=false;
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(!isStop) {
					LogKit.warn("开始扫描上线口接近开关信号");
					OperateResultExOne<Boolean> rackArriveEnterance = plc.ReadBool("I61.1");
					if(rackArriveEnterance.IsSuccess) {
						setRackArriveEnterance(rackArriveEnterance.Content.booleanValue());
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
		},"RackArriveEnterance-Scan-Thred").start();
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(!isStop) {
					
					OperateResultExOne<Boolean> rackArriveExit = plc.ReadBool("I61.0");
					if(rackArriveExit.IsSuccess) {
						setRackArriveExit(rackArriveExit.Content.booleanValue());
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
		},"RackArriveExit-Scan-Thred").start();
	}
	
	public void stop() {
		try {
			isStop=true;
			plc.ConnectClose();
			autoEnterScannerClient.stop();
			autoEixtScannerClient.stop();
		}catch(Throwable e) {
			LogKit.error(e.getMessage());
		} finally {
			isStop=true;
			exitlock.unlock();
			enterancelock.unlock();
		}
	}
}
